home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / template / ReferenceArray.h < prev    next >
C/C++ Source or Header  |  2001-04-18  |  4KB  |  154 lines

  1. // Copyright (C) 2000  Sean Cavanaugh
  2. // This file is licensed under the terms of the Lesser GNU Public License
  3. // (see LPGL.txt, or http://www.gnu.org/copyleft/lesser.txt)
  4.  
  5. #if !defined(AFX_ReferenceArray_H__BAEBCE9D_CD68_40AF_8A54_B23A0D14E807__INCLUDED_)
  6. #define AFX_ReferenceArray_H__BAEBCE9D_CD68_40AF_8A54_B23A0D14E807__INCLUDED_
  7.  
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif // _MSC_VER > 1000
  11.  
  12. #ifdef WIN32
  13. #define WIN32_LEAN_AND_MEAN
  14. #include "windows.h"
  15. #endif
  16. #include "assert.h"
  17. #include "limits.h"
  18.  
  19. #include "ReferenceCounter.h"
  20.  
  21. /*!
  22.   \author  Sean Cavanaugh
  23.   \email   sean@dimensionalrift.com
  24.   \cvsauthor $Author: sean $
  25.   \date    $Date: 2000/09/11 20:28:24 $
  26.   \version $Revision: 1.1 $ 
  27.   \brief   ReferenceArray is exactly the same as ReferencePtr, except it expects
  28.            the objects pointing to it to be allocated with new[] instead (and thusly
  29.            the object is deleted with delete[] when the reference count hits zero)
  30.            Additionally it provides a [] operator which returns a refernece to the
  31.            item in the list.  No bounds checking is performed.
  32.  
  33.            Arrays of basic data types (char, int, float, etc) should use ReferencePtr,
  34.            as delete[] is not required for them.  ReferencePtr can also handle void types
  35.            as a template parameter.
  36. */
  37. template<class DATA_T>
  38. class ReferenceArrayBlock
  39. {
  40. public:
  41.     DATA_T*            pData;                // User defined data block
  42.     mutable ReferenceCounter    ReferenceCount;
  43. };
  44.  
  45.  
  46. template<class DATA_T>
  47. class ReferenceArray  
  48. {
  49. public:
  50.     // Construction
  51.     ReferenceArray();
  52.     ReferenceArray(DATA_T* other);
  53.     ReferenceArray(const ReferenceArray<DATA_T>& other);
  54.     virtual ~ReferenceArray();
  55.  
  56.     // Assignment
  57.     ReferenceArray<DATA_T>& operator=(const ReferenceArray<DATA_T>& other);       
  58.     ReferenceArray<DATA_T>& operator=(DATA_T* other);
  59.  
  60.     // Dereferencing
  61.     operator DATA_T*() const {return m_pData->pData;}
  62.     DATA_T& operator[](unsigned int offset) const {return m_pData->pData[offset];}
  63.     DATA_T& operator[](int offset) const {return m_pData->pData[offset];}
  64.  
  65. protected:
  66.     // Internal methods
  67.     void Alloc();                // Allocate the m_pData
  68.     void Release();                // Releases a reference count (possibly freeing memory)
  69.  
  70. protected:
  71.     // Member data
  72.     ReferenceArrayBlock<DATA_T>* m_pData;
  73. };
  74.  
  75.  
  76. template<class DATA_T>
  77. ReferenceArray<DATA_T>::ReferenceArray()
  78. {
  79.     Alloc();
  80. }
  81.  
  82. template<class DATA_T>
  83. ReferenceArray<DATA_T>::ReferenceArray(DATA_T* other)
  84. {
  85.     Alloc();
  86.     m_pData->pData = other;
  87.     m_pData->ReferenceCount = 1;
  88. }
  89.  
  90. template<class DATA_T>
  91. ReferenceArray<DATA_T>::ReferenceArray(const ReferenceArray<DATA_T>& other)
  92. {
  93.     m_pData = other.m_pData;
  94.     m_pData->ReferenceCount++;
  95. }
  96.  
  97. template<class DATA_T>
  98. ReferenceArray<DATA_T>::~ReferenceArray()
  99. {
  100.     Release();
  101. }
  102.  
  103. template<class DATA_T>
  104. ReferenceArray<DATA_T>& ReferenceArray<DATA_T>::operator=(const ReferenceArray<DATA_T>& other)
  105. {
  106.     if (m_pData != other.m_pData)
  107.     {
  108.         Release();
  109.         m_pData = other.m_pData;
  110.         m_pData->ReferenceCount++;
  111.     }
  112.     return *this;
  113. }
  114.  
  115. template<class DATA_T>
  116. ReferenceArray<DATA_T>& ReferenceArray<DATA_T>::operator=(DATA_T* other)
  117. {
  118.     if (m_pData->ReferenceCount.dec() <= 0)
  119.     {
  120.         delete[] m_pData->pData;
  121.         m_pData->pData = other;
  122.         m_pData->ReferenceCount = 1;
  123.     }
  124.     else
  125.     {
  126.         Alloc();
  127.         m_pData->pData = other;
  128.     }
  129.     return *this;
  130. }
  131.  
  132. template<class DATA_T>
  133. void ReferenceArray<DATA_T>::Alloc()
  134. {
  135.     m_pData = new ReferenceArrayBlock<DATA_T>;
  136.     m_pData->ReferenceCount = 1;
  137.     m_pData->pData = NULL;
  138. }
  139.  
  140. template<class DATA_T>
  141. void ReferenceArray<DATA_T>::Release()
  142. {
  143.     assert(m_pData != NULL);
  144.     if (m_pData->ReferenceCount.dec() <= 0)
  145.     {
  146.         delete[] m_pData->pData;
  147.         m_pData->pData = NULL;
  148.         delete m_pData;
  149.         m_pData = NULL;
  150.     }
  151. }
  152.  
  153. #endif // !defined(AFX_ReferenceArray_H__BAEBCE9D_CD68_40AF_8A54_B23A0D14E807__INCLUDED_)
  154.